program define scurve_tvc
	version 12.1
	syntax [if] [in] , GENerate(name) AT(string) TVC(string) TEXP(string) [REPLACE TIES(name) shared(varname) strata(varname) graph plotopts(string)]
	capture drop _tscurve

	
	if "`strata'"=="" {
		if "`replace'"=="replace" {
		capture confirm new var `generate'
		if _rc==0 {
			display "(note: variable `generate' not found)"
		}
		else {
			drop `generate'
		}
	}
	confirm new variable `generate'
	preserve
	tempvar basehc b_x H_x _xb_t
	tempfile scurve
	tempname xb coef
	capture keep `if'
	capture keep `in'
	display as text _newline(1) "Dataset has been temporarily split at failure times"
	stsplit, at(failures)
	local i=1
	local tvc_vars ""
	tokenize `tvc'
	while "``i''" != "" { 						
		capture confirm new var _``i''_t
		if _rc!=0 {
			display in red "This command needs to create interaction variables using the variables listed in at(). These new variables are called _varname_t. Please rename any existing variables which have the same name." 
			confirm new var _``i''_t
		}
		gen _``i''_t = ``i'' * `texp'
		local tvc_vars "`tvc_vars' _``i''_t"
		local ++i
	}
	local i=1
	local est_vars " "
	tokenize `at'
	while "``i''" != "" {
		local est_vars "`est_vars' ``i''"
		local i=`i'+2
	}
	if "`shared'"!="" {
		local sharedopt "shared(`shared')"
	}
	else {
		local sharedopt ""
	}
	display _newline(1) "The estimation is based on the following Cox Proportional Hazards Model:"
	stcox `est_vars' `tvc_vars', nohr `ties' `sharedopt'
	display "Note: tvc-interactions denoted by _varname_t were interacted with `texp'."
	matrix b=e(b)
	quietly predict `basehc', basehc
	sort _t
	collapse (mean) `basehc', by(_t)
	local i=1
	local j=2
	scalar `xb'=0
	tokenize `at'
	while "``i''" != "" {
		local colnum=colnumb(b,"``i''")
		scalar `xb' = `xb' + b[1,`colnum'] * ``j''
		tempname ``i''_value
		scalar ```i''_value' = ``j''
		local i=`i'+2
		local j=`j'+2
	}
	local k=1
	quietly gen `_xb_t'=0
	tokenize `tvc'
	while "``k''" != "" {
		local tvc_name "_``k''_t"
		local colnum=colnumb(b,"`tvc_name'")
		scalar `coef' = b[1,`colnum']
		quietly replace `_xb_t' = `_xb_t' + `coef' * ```k''_value' * `texp'
		local ++k
	}
	quietly gen `b_x' = 1-(1-`basehc')^exp(`xb'+`_xb_t') 
	quietly gen `H_x' = sum(`b_x')
	quietly gen `generate' = exp(-`H_x')
	keep `generate' _t
	rename _t _tscurve
	label var _tscurve "analysis time"
	label var `generate' ""
	quietly save `scurve'
	restore
	tempvar merge
	quietly merge using `scurve', _merge(`merge')
		if "`graph'"=="graph" {
		twoway line `generate' _tscurve, ytitle("") title("Cox proportional hazards regression") c(J) `plotopts'
	}
	}
	
	
	else {
	quietly levelsof `strata', local(stratavalues)
	if "`replace'"=="replace" {
		foreach s of local stratavalues { 
			capture confirm new var `generate'`s'
			if _rc==0 {
				display "(note: variable `generate'`s' not found)"
			}
			else {
				drop `generate'`s'
			}
		}
	}
	foreach s of local stratavalues { 
	confirm new variable `generate'`s'
	}
	preserve
	tempvar basehc _xb_t
	tempfile scurve
	tempname xb coef
	capture keep `if'
	capture keep `in'
	display as text _newline(1) "Dataset has been temporarily split at failure times"
	stsplit, at(failures)
	local i=1
	local tvc_vars ""
	tokenize `tvc'
	while "``i''" != "" { 						
		capture confirm new var _``i''_t
		if _rc!=0 {
			display in red "This command needs to create interaction variables using the variables listed in at(). These new variables are called _varname_t. Please rename any existing variables which have the same name." 
			confirm new var _``i''_t
		}
		gen _``i''_t = ``i'' * `texp'
		local tvc_vars "`tvc_vars' _``i''_t"
		local ++i
	}
	local i=1
	local est_vars " "
	tokenize `at'
	while "``i''" != "" {
		local est_vars "`est_vars' ``i''"
		local i=`i'+2
	}
	if "`shared'"!="" {
		local sharedopt "shared(`shared')"
	}
	else {
		local sharedopt ""
	}
	display _newline(1) "The estimation is based on the following Cox Proportional Hazards Model:"
	stcox `est_vars' `tvc_vars', nohr `ties' `sharedopt' strata(`strata')
	display "Note: tvc-interactions denoted by _varname_t were interacted with `texp'."
	matrix b=e(b)
	quietly predict `basehc', basehc
	quietly levelsof `strata', local(stratavalues)
	sort _t
	collapse (mean) `basehc', by(_t `strata')
	quietly reshape wide `basehc', i(_t) j(`strata')
	local i=1
	local j=2
	scalar `xb'=0
	tokenize `at'
	while "``i''" != "" {
		local colnum=colnumb(b,"``i''")
		scalar `xb' = `xb' + b[1,`colnum'] * ``j''
		tempname ``i''_value
		scalar ```i''_value' = ``j''
		local i=`i'+2
		local j=`j'+2
	}
	local k=1
	quietly gen `_xb_t'=0
	tokenize `tvc'
	while "``k''" != "" {
		local tvc_name "_``k''_t"
		local colnum=colnumb(b,"`tvc_name'")
		scalar `coef' = b[1,`colnum']
		quietly replace `_xb_t' = `_xb_t' + `coef' * ```k''_value' * `texp'
		local ++k
	}
	local plot_j ""
	local plot_vars ""
	foreach s of local stratavalues { 
	tempvar b_x`s' H_x`s' 
	quietly gen `b_x`s'' = 1-(1-`basehc'`s')^exp(`xb'+`_xb_t') 
	quietly gen `H_x`s'' = sum(`b_x`s'')
	quietly gen `generate'`s' = exp(-`H_x`s'')
	label var `generate'`s' "`strata' = `s'"
	local plot_j = "`plot_j' " + "J"
	local plot_vars = "`plot_vars' " + "`generate'`s'"
	}
	keep `generate'* _t
	rename _t _tscurve
	label var _tscurve "analysis time"
	quietly save `scurve'
	restore
	tempvar merge
	quietly merge using `scurve', _merge(`merge')
	if "`graph'"=="graph" {
		twoway line `plot_vars' _tscurve, title("Cox proportional hazards regression") c(`plot_j') `plotopts'
	}
	}
end
